<template>
  <div class="box">
    <el-form label-width="80px" inline size="small">
      <el-form-item label="学科名称">
        <el-input
          v-model="page.subjectName"
          placeholder="请输入内容"
        ></el-input>
      </el-form-item>
      <el-button size="small" @click="btnClear">清除</el-button>
      <el-button @click="getSubList1" type="primary" size="small" class="search"
        >搜索</el-button
      >
      <el-button
        style="float: right"
        icon="el-icon-edit"
        type="success"
        size="small"
        @click="addSub"
        >新增学科</el-button
      >
    </el-form>

    <el-alert :title="elalert" type="info" show-icon :closable="false" />
    <!-- 表格 -->
    <el-card>
      <el-table :data="list">
        <el-table-column
          stripe
          width="80"
          label="序号"
          type="index"
          align="center"
        ></el-table-column>
        <el-table-column
          width="80"
          prop="subjectName"
          label="学科名称"
          align="center"
        ></el-table-column>
        <el-table-column
          width="120"
          prop="username"
          label="创建者"
          align="center"
        ></el-table-column>
        <el-table-column
          width="160"
          prop="addDate"
          label="创建日期"
          align="center"
        >
          <template v-slot="{ row }">
            <!-- 将时间进行格式化 -->
            {{ row.addDate | formatDate1 }}
          </template>
        </el-table-column>
        <el-table-column
          width="120"
          prop="isFrontDisplay"
          label="前台是否显示"
          :formatter="formatDisplay"
          align="center"
        ></el-table-column>
        <el-table-column
          width="80"
          prop="twoLevelDirectory"
          label="二级目录"
          align="center"
        ></el-table-column>
        <el-table-column
          width="80"
          prop="tags"
          label="标签"
          align="center"
        ></el-table-column>
        <el-table-column
          width="80"
          prop="totals"
          label="题目数量"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="{ row }">
            <el-button
              type="text"
              @click="$router.push(`directorys?id=${row.id}`)"
              >学科分类</el-button
            >
            <el-button type="text" @click="$router.push(`tags?id=${row.id}`)"
              >学科标签</el-button
            >
            <el-button type="text" @click="editSub(row.id)">修改</el-button>
            <el-button type="text" @click="delSub(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        class="pagination"
        background
        :current-page="page.page"
        :page-size="page.pagesize"
        :page-sizes="[3, 5, 8, 10]"
        layout="prev, pager, next,sizes,jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="changePage"
      />
    </el-card>

    <!-- 弹窗 -->
    <sub-add
      ref="subAdd"
      :showDialog.sync="showDialog"
      @getSubList="getSubList()"
    />
  </div>
</template>

<script>
import { getSubList, delSub } from "../../api/hmmm/subjects";
import { formatDate } from "./filters";
import SubAdd from "../components/subjects-add.vue";
import SubjectEnum from "../../api/base/baseApi";
export default {
  // 注册过滤器
  filters: {
    formatDate1: formatDate,
  },
  components: {
    SubAdd,
  },
  data() {
    return {
      list: [], // 接收数据
      page: {
        page: 1, // 当前页码
        pagesize: 10, // 每页展示条数
        total: 0, // 总数
        subjectName: "",
      },
      elalert: "",
      showDialog: false,
    };
  },
  created() {
    this.getSubList();
    this.formatDisplay();
  },
  methods: {
    // 获取学科列表
    async getSubList() {
      const { data } = await getSubList(this.page);
      this.page.total = data.counts;
      this.list = data.items;
      this.elalert = `数据一共${data.counts}条`;
    },
    // 最新码数
    changePage(newPage) {
      this.page.page = newPage;
      this.getSubList();
    },
    // 每页条数更新
    handleSizeChange(newSize) {
      if (this.subjectName) return;
      this.page.pagesize = newSize;
      this.getSubList();
    },
    // 新增学科
    addSub() {
      this.showDialog = true;
    },
    // 删除学科
    async delSub(id) {
      try {
        // 提示框
        await this.$confirm("此操作将永久删除该学科, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
        await delSub(id);
        this.$message.success("删除成功");
        if (this.list.length === 1) {
          this.page.page = this.page.page - 1;
        }
        this.getSubList();
      } catch (error) {
        console.log(error);
      }
    },
    // 编辑学科
    editSub(id) {
      this.$refs.subAdd.getSub(id);
      this.showDialog = true;
    },
    // 搜索数据
    async getSubList1() {
      this.getSubList();
    },
    // 点击清除
    btnClear() {
      this.page.subjectName = "";
      this.getSubList1();
    },
    // 格式化前台是否显示形式
    formatDisplay(row, column, cellValue, index) {
      // cellValue是每一行是否显示所对应的值
      const obj = SubjectEnum.isShow.find((item) => item.id === cellValue);
      return obj ? obj.value : "未知";
    },
  },
};
</script>

<style scoped>
.box {
  background-color: #fff;
  padding-top: 26px;
  padding-left: 27px;
  padding-right: 27px;
}
:deep(.el-input__inner) {
  height: 33px;
}
.el-alert {
  margin-bottom: 20px;
}

:deep(.el-col-15) {
  display: flex;
  justify-content: end;
}
.pagination {
  display: flex;
  justify-content: end;
  margin-top: 20px;
}
:deep(.el-table th.is-leaf) {
  background-color: #fafafa;
  border-bottom: 2px solid #e8e8e8;
}
</style>
